package org.edu.modules.stu.biye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.mapper.VeBaseStudentMapper;
import org.edu.modules.stu.biye.entity.VeStuGraduation;
import org.edu.modules.stu.biye.entity.VeStuGraduationAuditConfig;
import org.edu.modules.stu.biye.mapper.VeStuGraduationAuditConfigMapper;
import org.edu.modules.stu.biye.mapper.VeStuGraduationAuditInfoMapper;
import org.edu.modules.stu.biye.entity.VeStuGraduationAuditInfo;
import org.edu.modules.stu.biye.dto.VeStuGraduationAuditInfoDto;
import org.edu.modules.stu.biye.mapper.VeStuGraduationMapper;
import org.edu.modules.stu.biye.service.VeStuGraduationAuditInfoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 实现类
 */
@Service
public class VeStuGraduationAuditInfoServiceImpl extends BaseServiceImpl<VeStuGraduationAuditInfo, VeStuGraduationAuditInfoMapper> implements VeStuGraduationAuditInfoService {


    @Autowired
    private VeStuGraduationAuditConfigMapper veStuGraduationAuditConfigMapper;

    @Autowired
    private VeBaseStudentMapper veBaseStudentMapper;

    @Autowired
    private VeStuGraduationMapper veStuGraduationMapper;

    private static final List<String> AUDIT_TYPES = Arrays.asList("BZR", "CJ", "CW", "XSC");

    public IPage<VeStuGraduationAuditInfoDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper) {
        return mapper.pageDto(page, queryWrapper);
    }

    /**
     * 查询实体类
     */
    public VeStuGraduationAuditInfoDto findDtoById(String id) {
        VeStuGraduationAuditInfo entity = super.findById(id);
        VeStuGraduationAuditInfoDto dto = ConvertUtils.convertToDto(entity, VeStuGraduationAuditInfoDto.class);
        return dto;
    }

    /**
     * 新增或者修改
     */
    public void insertOrUpdate(VeStuGraduationAuditInfoDto dto, Boolean isAdd) {
        if (isAdd) {
            super.insert(dto);
        } else {
            super.update(dto);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditByBZR(VeStuGraduationAuditInfoDto dto) {
        //如果是待审核，需要添加4条数据，如果是0不通过，1通过，说明已经有数据了，更改数据就好。
        // 定义审核类型顺序（按业务需求调整顺序）
        // 遍历审核类型列表
        if (dto.getId() == null) {
            for (String auditType : AUDIT_TYPES) {
                VeStuGraduationAuditInfo veStuGraduationAuditInfo = new VeStuGraduationAuditInfo();
                veStuGraduationAuditInfo.setStuId(dto.getStuId());
                veStuGraduationAuditInfo.setAuditType(auditType);
                if (auditType.equals("BZR")) {
                    veStuGraduationAuditInfo.setAuditStatus(dto.getAuditStatus());
                    veStuGraduationAuditInfo.setRemark(dto.getRemark());
                } else {
                    veStuGraduationAuditInfo.setAuditStatus(-1);
                }
                veStuGraduationAuditInfo.setCreateuserid(dto.getCreateuserid());
                // 执行插入操作
                mapper.insert(veStuGraduationAuditInfo);
            }
        } else {
            // 如果是0不通过，1通过，说明已经有数据了，更改数据就好
            VeStuGraduationAuditInfo veStuGraduationAuditInfo = new VeStuGraduationAuditInfo();
            veStuGraduationAuditInfo.setAuditStatus(dto.getAuditStatus());
            veStuGraduationAuditInfo.setRemark(dto.getRemark());
            veStuGraduationAuditInfo.setId(dto.getId());
            mapper.updateById(veStuGraduationAuditInfo);
        }
    }

    @Override
    public void insertGraduatedStudent(Integer stuId) {
        if (canStudentGraduate(stuId)) {
            insertStudentToGraduation(stuId);
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList) {
        for (Map map : mapList) {
            VeStuGraduationAuditInfoDto dto = new VeStuGraduationAuditInfoDto();
            //         dto.setStuId ((String)map.get(0000));
            //         dto.setAuditType ((String)map.get(0000));
            //         dto.setAuditStatus ((String)map.get(0000));
            //         dto.setRemark ((String)map.get(0000));
            //         dto.setCreateuserid ((String)map.get(0000));
            //         dto.setTerminalid ((String)map.get(0000));

            super.insert(dto);
        }

    }

    //从配置表中获取启用的审核类型
    public List<String> getEnabledAuditTypes() {
        //查询毕业审核配置表，就一条数据，所以id传1
        VeStuGraduationAuditConfig veStuGraduationAuditConfig = veStuGraduationAuditConfigMapper.selectById(1);
        List<String> enabledTypes = new ArrayList<>();
        if (veStuGraduationAuditConfig != null) {
            if (veStuGraduationAuditConfig.getBzr() != null && veStuGraduationAuditConfig.getBzr() == 1)
                enabledTypes.add("BZR");
            if (veStuGraduationAuditConfig.getCj() != null && veStuGraduationAuditConfig.getCj() == 1)
                enabledTypes.add("CJ");
            if (veStuGraduationAuditConfig.getCw() != null && veStuGraduationAuditConfig.getCw() == 1)
                enabledTypes.add("CW");
            if (veStuGraduationAuditConfig.getXsc() != null && veStuGraduationAuditConfig.getXsc() == 1)
                enabledTypes.add("XSC");
        }
        return enabledTypes;
    }

    //检查特定学生的特定审核类型是否通过
    public boolean isAuditPassed(Integer stuId, String auditType) {
        QueryWrapper<VeStuGraduationAuditInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", stuId).eq("audit_type", auditType).eq("audit_status", 1);
        return mapper.selectCount(queryWrapper) > 0;
    }

    //检查学生是否可以毕业
    public boolean canStudentGraduate(Integer stuId) {
        List<String> enabledAuditTypes = getEnabledAuditTypes();
        for (String type : enabledAuditTypes) {
            if (!isAuditPassed(stuId, type)) {
                return false;
            }
        }
        return true;
    }

    //将学生数据插入毕业表
    public void insertStudentToGraduation(Integer stuId) {
        //根据stuId获取学生数据
        VeBaseStudent veBaseStudent = veBaseStudentMapper.selectById(stuId);
        VeStuGraduation veStuGraduation = new VeStuGraduation();
        BeanUtils.copyProperties(veBaseStudent, veStuGraduation);
        veStuGraduation.setStuId(veBaseStudent.getId());
        veStuGraduation.setIdcard(veBaseStudent.getSfzh());
        veStuGraduation.setName(veBaseStudent.getXm());
        veStuGraduation.setSex(veBaseStudent.getXbm());
        veStuGraduation.setFalName(veBaseStudent.getFalText());
        veStuGraduation.setSpecName(veBaseStudent.getSpecText());
        veStuGraduation.setBjName(veBaseStudent.getBjText());
        veStuGraduation.setCode(veBaseStudent.getXh());
        veStuGraduation.setRxDate(veBaseStudent.getRxny());
        veStuGraduationMapper.insert(veStuGraduation);
    }

}
